package com.fan.leetcode.stackandqueue;

import java.util.Stack;

/**
 * @author :  PF_23
 * @Description : 用栈实现括号匹配
 * "()[]{}"
 * <p>
 * Output : true
 * @date : 2020/02/26.
 */

public class Num04 {

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (char c : s.toCharArray()) {
            if (c == '(' || c == '{' || c == '[') {
                stack.push(c);
            } else {
                if (stack.isEmpty()) {
                    return false;
                }
                char cStack = stack.pop();
                boolean b1 = c == ')' && cStack == '(';
                boolean b2 = c == ']' && cStack == '[';
                boolean b3 = c == '}' && cStack == '{';
                if (b1 || b2 || b3) {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}

